package thread;

public class Demo7 {
    public static void main(String[] args)throws InterruptedException {
        Thread thread=new Thread(()->{
            while (true){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        //这样设置后台线程必须放在start之前
        //主线程结束后台线程也将会结束
        thread.setDaemon(true);
        thread.start();
        for (int i = 0; i <3 ; i++) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
        System.out.println("main 结束");
    }
}
